<?php

namespace Acme\FirmsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;
class AdminFirmsForm extends AbstractType {

  public function buildForm(FormBuilder $builder, array $options) {
    $builder->add('name', 'text', array('label' => 'Название фирмы'))
            ->add('description', 'textarea', array('label' => 'Описание'))
            ->add('category', 'entity', array('label' => 'Категория',
                                              'class' => 'AcmeFirmsBundle:CategoryFirms')
                 )
            ->add('user', 'entity', array('label' => 'Пользователь',
                                          'class' => 'AcmeFirmUsersBundle:User',
                                          'query_builder' => function (EntityRepository $er){
                                                                return $er->createQueryBuilder('u')
                                                                          ->innerJoin('u.userRoles', 'ur')
                                                                          ->where('ur.name =  ?1')
                                                                          ->setParameter(1, 'ROLE_FIRM')
                                                                          ->orderBy('u.username', 'ASC');
                                          }));
                                          
  }

  public function getDefaultOptions(array $options) {
    return array(
        'data_class' => 'Acme\FirmsBundle\Entity\Firms',
        'csrf_protection' => true,
        'csrf_field_name' => '_token',
        // a unique key to help generate the secret token
        'intention'       => 'id'
    );
  }

  public function getName() {
    return 'admin_firms_form';
  }

}
